﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Jt808DataParser.Utils
{
    public class BuildMessageBody
    {
        public static byte[] build8103MessageBody(Dictionary<int,object> paramsDic)
        {
            try
            {
                //Number of parameters
                int paramCount = paramsDic.Count();
                List<byte> byteList = new List<byte>();
                byteList.Add((byte)paramCount);
                foreach (int key in paramsDic.Keys)
                {
                    //Parameter ID takes up 4 bytes+parameter length is 1 byte
                    byteList.AddRange(CommonUtil.Int32ToBytes(key));
                    switch (key)
                    {
                        case 0x0001:
                        case 0x0018:
                        case 0x0027:
                        case 0x0029:
                        case 0x0030:
                        case 0x0055:
                        case 0x0056:
                        case 0x0080:
                        case 0xF005:
                        case 0xF018:
                        case 0xF01D:
                        case 0xF01E:
                        case 0xF022:
                        case 0xF028:
                        case 0xF02B:
                        case 0xF02E:
                        case 0xF02F:
                            byteList.Add(4);
                            byteList.AddRange(CommonUtil.Int32ToBytes((Int32)paramsDic[key]));
                            break;
                        case 0x0010:
                        case 0x0011:
                        case 0x0012:
                        case 0x0013:
                        case 0x0017:
                        case 0xF000:
                        case 0xF004:
                        case 0xF030:
                            byteList.Add((byte)paramsDic[key].ToString().Length);
                            byteList.AddRange(Encoding.Default.GetBytes(paramsDic[key].ToString()));
                            break;
                        case 0xF001:
                        case 0xF002:
                        case 0xF003:
                        case 0xF00E:
                        case 0xF013:
                        case 0xF015:
                        case 0xF01C:
                        case 0xF021:
                        case 0xF024:
                        case 0xF025:
                        case 0xF026:
                        case 0xF027:
                            byteList.Add(2);
                            byteList.AddRange(CommonUtil.ShortToBytes((short)paramsDic[key]));
                            break;
                        case 0xF006:
                        case 0xF007:
                        case 0xF008:
                        case 0xF009:
                        case 0xF00A:
                        case 0xF00B:
                        case 0xF00C:
                        case 0xF00D:
                        case 0xF00F:
                        case 0xF010:
                        case 0xF011:
                        case 0xF012:
                        case 0xF014:
                        case 0xF016:
                        case 0xF017:
                        case 0xF019:
                        case 0xF01A:
                        case 0xF01B:
                        case 0xF01F:
                        case 0xF020:
                        case 0xF023:
                        case 0xF029:
                        case 0xF02A:
                        case 0xF02C:
                        case 0xF02D:
                            byteList.Add(1);
                            byteList.Add((byte)paramsDic[key]);
                            break;
                        default:
                            break;
                    }
                }
                return byteList.ToArray();
            }
            catch (Exception ex) {
                return null;
            }
        }
    }
}
